
import { assetManager, ImageAsset, isValid, SpriteFrame, Texture2D } from "cc";
import * as fgui from "fairygui-cc";
import UrlEnum from "../Res/UrlEnum";
import Log from "../Log/Log";
export class ExternalGLoader extends fgui.GLoader {
    private _urlDict: { [key: string]: any } = {};
    private _url: string = "";
    public constructor() {
        super();
    }
    /**
     * 加载外部
     */
    protected loadExternal(): void {
        if (this.url.indexOf(`https://`) != -1 || this.url.indexOf(`http://`) != -1) {
        } else {
            if (this.url.indexOf('res/image/') == -1) {
                this.url = `res/image/${this.url}`;
                return;
            }
        }
        this._url = this.url;
        let url = this.url, urlH: string;
        if (this.url.indexOf(`https://`) != -1 || this.url.indexOf(`http://`) != -1) {
            urlH = this.url;
        } else {
            urlH = UrlEnum.resUrl + this.url;
        }
        if (this._urlDict[urlH]) {
            if (this._urlDict[urlH] instanceof SpriteFrame) {
                this.onExternalLoadSuccess(this._urlDict[urlH]);
            }
            else if (this._urlDict[urlH] instanceof Texture2D) {
                let sf = new SpriteFrame();
                sf.texture = this._urlDict[urlH];
                this.onExternalLoadSuccess(sf);
                sf = null;
            }
            else if (this._urlDict[urlH] instanceof ImageAsset) {
                let texture = new Texture2D();
                texture.image = this._urlDict[urlH];
                let sf = new SpriteFrame();
                sf.texture = texture;
                this.onExternalLoadSuccess(sf);
                texture = null;
                sf = null;
            }
            return;
        }
        assetManager.loadRemote<ImageAsset>(`${urlH}`, { ext: ".png" }, (err, asset: ImageAsset) => {
            if (this._url != url || !isValid(this._node))
                return;
            if (err) {
                Log.ins.log(3, `位图资源加载出错`, err);
                return;
            }
            this._urlDict[urlH] = asset;
            if (asset instanceof SpriteFrame) {
                this.onExternalLoadSuccess(asset);
                asset = null;
            }
            else if (asset instanceof Texture2D) {
                let sf = new SpriteFrame();
                sf.texture = asset;
                this.onExternalLoadSuccess(sf);
                sf = null;
            }
            else if (asset instanceof ImageAsset) {
                let texture = new Texture2D();
                texture.image = asset;
                let sf = new SpriteFrame();
                sf.texture = texture;
                this.onExternalLoadSuccess(sf);
                texture = null;
                sf = null;
            }
            else {
                Log.ins.log(3, `GLoader:cant load`, this.url);
            }
        });
    }
}